package com.example.demo.config;

import com.example.demo.interceptor.AuthenticationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author KUBUB
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册身份验证拦截器
        InterceptorRegistration registration = registry.addInterceptor(authenticationInterceptor());
        // 所有路径都被拦截
        registration.addPathPatterns("/**");
        // 添加不拦截路径
        List<String> excludePath = new ArrayList<>();
        excludePath.add("/doc.html");
        excludePath.add("/swagger-resources");
        excludePath.add("/swagger-resources/**");
        excludePath.add("/v2/api-docs");
        excludePath.add("/webjars/**");
        registration.excludePathPatterns(excludePath);
    }

    /**
     * 身份验证拦截器
     *
     * @return
     */
    @Bean
    public AuthenticationInterceptor authenticationInterceptor() {
        return new AuthenticationInterceptor();
    }
}
